package com.mdx.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtil {

    public String createToken(Map<String,String> map){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE,30);
        JWTCreator.Builder builder = JWT.create();
        for (String key: map.keySet()){
            builder.withClaim(key,map.get(key));
        }
        String token = builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256("lovo!"));
        return token;
    }
    public String verity(String token,String key){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("lovo!")).build();
        DecodedJWT decodedJWT = jwtVerifier.verify(token);
        String value = decodedJWT.getClaim(key).asString();
        return value;
    }
}
